package com.ruoyi.web.controller.topic;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.system.domain.vo.DetailedArticle;
import com.ruoyi.system.domain.vo.FirstPageArticle;
import com.ruoyi.system.domain.vo.Result;
import com.ruoyi.system.domain.vo.ShowArticle;
import com.ruoyi.system.service.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "首页模块")
@RestController
@RequestMapping("/api")
public class IndexController extends BaseController {

    @Autowired
    private ArticleService articleService;

    @ApiOperation("首页展示所有话题")
    @PostMapping({"/","/index/{pageNum}"})
    public Result<List<FirstPageArticle>> indexArticles(@PathVariable("pageNum") Integer pageNum){
        PageHelper.startPage(pageNum,6);
        List<FirstPageArticle> articles = articleService.getFirstAllArticles();
        PageInfo<FirstPageArticle> pageInfo = new PageInfo<>(articles);
        return Result.success(articles);
    }

    @ApiOperation("跳转话题详情")
    @GetMapping("/article/{id}")
    public Result<DetailedArticle> article(@PathVariable("id") Long id){
        DetailedArticle article = articleService.getDetailedArticle(id);
        return Result.success(article);
    }

    @ApiOperation("查询总计访问量")
    @GetMapping("/footer/articleMessage")
    public Result<Integer> articlesMessage(){
        return Result.success(articleService.getArticleViewTotal());
    }

        @GetMapping("/articles/tagId/{tagId}")
    @ApiOperation("根据标签id查询话题")
    public Result<List<ShowArticle>> getArticleByTypeId(@PathVariable("tagId") Long tagId){
        /*ShowArticle articleById = articleService.getArticleById(id);*/
            return Result.success(articleService.getArticleTagById(tagId));
    }

    @ApiOperation("搜索话题")
    @GetMapping("/search/{pageNum}/{query}")
    public Result<List<FirstPageArticle>> getSearchArticles(@PathVariable("query") String query,
                                                            @PathVariable("pageNum") Integer pageNum){
        PageHelper.startPage(pageNum,6);
        List<FirstPageArticle> searchArticles = articleService.getSearchArticles(query);
        PageInfo<FirstPageArticle> pageInfo = new PageInfo<>(searchArticles);
        return Result.success(searchArticles);
    }

    @ApiOperation("根据tagId嵌套搜索文章")
    @GetMapping("/search/{tagId}/{pageNum}/{query}")
    public Result<List<FirstPageArticle>> getArticleByInfo(@PathVariable("query") String query,
                                                           @PathVariable("tagId") Long tagId,
                                                           @PathVariable("pageNum") Integer pageNum){
        PageHelper.startPage(pageNum,1000);
        List<FirstPageArticle> searchArticles = articleService.getArticleInfoId(query,tagId);
        PageInfo<FirstPageArticle> pageInfo = new PageInfo<>(searchArticles);
        return Result.success(searchArticles);
    }

    @ApiOperation("根据tagId搜素文章")
    @GetMapping("/search/index/{tagId}")
    public Result<List<FirstPageArticle>> getArticleByInfo(@PathVariable("tagId") Long tagId,
                                                           @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum){
        PageHelper.startPage(pageNum,1000);
        List<FirstPageArticle> searchArticles = articleService.getArticleTagId(tagId);
        PageInfo<FirstPageArticle> pageInfo = new PageInfo<>(searchArticles);
        return Result.success(searchArticles);
    }
}

